package com.weather;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class ViewWeatherServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String timeperiod=req.getParameter("timeperiod");
		System.out.println(timeperiod);
		String start=req.getParameter("start");
		String end=req.getParameter("end");
		System.out.println(start);
		System.out.println(end);
		if(start==null||end==null){
			Date d=new Date();
			start=Long.toString(d.getTime()-1000*3600*24);
			end=Long.toString(d.getTime());
		}
		int strName=5;
		Query query = new Query(timeperiod).addFilter("timestamp", Query.FilterOperator.GREATER_THAN, Long.parseLong(start));
		query.addFilter("timestamp", Query.FilterOperator.LESS_THAN, Long.parseLong(end));
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		List<Entity> greetings = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
		JSONArray timelable=new JSONArray();
		JSONArray temperature=new JSONArray();
		JSONArray humidity=new JSONArray();
		JSONArray wind=new JSONArray();
		double sum_temp=0;
		double sum_hum=0;
		double sum_wind=0;
		int i;
		JSONObject object = new JSONObject();
		for(i=0;i<=greetings.size()-1;i++){
			Date d=new Date();
			d.setTime(Long.parseLong(greetings.get(i).getProperty("timestamp").toString()));
			String strTime="";
			if(timeperiod.equals("WeatherInfo")){
				strTime=(8+d.getHours())%24+":"+d.getMinutes();
			}
			else if(timeperiod.equals("WeatherInfoDay")){
				strTime=d.getMonth()+"."+d.getDate();
			}
			else if(timeperiod.equals("WeatherInfoMonth")){
				strTime=""+(d.getMonth()+1);
			}
			sum_temp+=Double.parseDouble(greetings.get(i).getProperty("temp").toString());
			sum_hum+=Double.parseDouble(greetings.get(i).getProperty("SD").toString().replace("%", ""));
			sum_wind+=Double.parseDouble(greetings.get(i).getProperty("WSE").toString());
			timelable.put(strTime);
			try {
				temperature.put(Double.parseDouble(greetings.get(i).getProperty("temp").toString()));
				humidity.put(Double.parseDouble(greetings.get(i).getProperty("SD").toString().replace("%", "")));
				wind.put(Double.parseDouble(greetings.get(i).getProperty("WSE").toString()));
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			/*if(i!=greetings.size()-1){
				timelable=timelable+"\""+strTime+"\",";
				temperature=temperature+greetings.get(i).getProperty("temp").toString()+",";
				humidity=humidity+greetings.get(i).getProperty("SD").toString().replace("%", "")+",";
				wind=wind+greetings.get(i).getProperty("WSE").toString()+",";
			}
			else{
				timelable=timelable+"\""+strTime+"\"";
				temperature=temperature+greetings.get(i).getProperty("temp").toString();
				humidity=humidity+greetings.get(i).getProperty("SD").toString().replace("%", "");
				wind=wind+greetings.get(i).getProperty("WSE").toString();
			}*/
			
			
		}
		System.out.println(timelable);
		System.out.println(temperature);
		sum_temp/=i;
		sum_hum/=i;
		sum_wind/=i;
		/*System.out.println(sum_temp);
		System.out.println(sum_hum);
		System.out.println(sum_wind);*/
		/*timelable+="]";
		temperature+="]";
		humidity+="]";
		wind+="]";*/
		try {
			object.put("timelabel", timelable);
			object.put("temp", temperature);
			object.put("humidity",humidity);
			object.put("wind", wind);
			object.put("sum_temp", sum_temp);
			object.put("sum_hum", sum_hum);
			object.put("sum_wind", sum_wind);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			object.put("temp", temperature);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*System.out.println(timelable);
		System.out.println(temperature);
		System.out.println(humidity);
		System.out.println(wind);*/
		resp.getWriter().print(object);
	}
}
